# 实现预期结果与实际结果的比较
class ExpectActualCompare():

    # 实现预期结果与实际结果的比较
    def expect_actual_compare(self, flag, expect_value, actual_value):
        if flag == True:
             __result = self.expect_actual_compare_json(expect_value, actual_value)
        elif flag == False:
            __result = self.expect_actual_compare_text(expect_value, actual_value)

        # 两个都为空值的情况下（下面两个方法都进行了抵消操作，两个都为空就表明返回数据完全相同）
        if not(__result[0]) and not(__result[1]):
            return "预期与实际一致"

        return "expect:%s\nactual:%s" % (__result[0], __result[1])

    # 实现json数据的比较
    def expect_actual_compare_json(self, expect_value, actual_value):
        expect = {}
        actual = {}

        set1 = set(list(expect_value.keys()) + list(actual_value.keys()))
        for key in set1:
            if key not in expect_value.keys():

                actual[key] = actual_value[key]
            elif key not in actual_value.keys():
                expect[key] = expect_value[key]
            else:
                if expect_value[key] != actual_value[key]:
                  expect[key] = expect_value[key]
                  actual[key] = actual_value[key]
        return (expect, actual)

    # 实现text对象格式数据的比较
    def expect_actual_compare_text(self, expect_value, actual_value):
        get_result = self.expect_actual_compare_json(expect_value, actual_value)
        get_expect = get_result[0]['text']
        get_actual = get_result[1]['text']
        if get_expect in get_actual:
            del get_actual['text']
            del get_expect['text']
        else:
            del get_actual['text']
        return (get_expect, get_actual)



